package com.lujieni.springbooteasyexcel.service;

import com.lujieni.springbooteasyexcel.server.WebSocketServer;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.websocket.Session;
import java.io.IOException;

/**
 * @Author：小鹿爱吃鱼
 * @Date: 2025/5/1 17:57
 * @Description:
 */
@Service
@RequiredArgsConstructor
public class SendMsgService {

    private final WebSocketServer webSocketServer;

    @Async
    public String sendMessageToSpecificClient(String message, String userCode){
        try {
            Session session = webSocketServer.checkIsOnline(userCode);
            if(session != null){
                webSocketServer.sendMessageToSpecificClient(message, session);
                return "发送成功";
            }else{
                return "用户不在线,无法发送";
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}
